package main

import (
	"fmt"
	"time"
)

func longestCommonPrefix(strs []string) string {
	if strs[0] == "" {
		return ""
	}
	if len(strs) <= 1 {
		return strs[0]
	}
	stringlen := 0
	strslen := len(strs)
	result := []string{}
	cmpstr := strs[0]
	for i := 1; i < strslen; i++ {
		if len(strs[i]) > len(cmpstr) {
			stringlen = len(cmpstr)
			result = append(result[:0], cmpstr)
		} else {
			stringlen = len(strs[i])
			result = append(result[:0], strs[i])
		}
		fmt.Println(stringlen)

		for j := 0; j < stringlen; j++ {
			fmt.Println("strs[i][j]", strs[i][j], "  cmpstr[j]", cmpstr[j])

			if strs[i][j] != cmpstr[j] {
				result = append(result[:0], cmpstr[:j])
				fmt.Println(result)
				fmt.Println()
				break
			} else {
				continue
			}
		}
		cmpstr = result[0]

	}
	return result[0]

}

func main() {
	start := time.Now()
	// s := []string{"flower", "flow", "flight"}
	// s := []string{"ab", "a"}
	// s := []string{"dog", "racecar", "car"}
	s := []string{""}
	result := longestCommonPrefix(s)
	cost := time.Since(start)
	fmt.Println("result:", result)
	fmt.Println(cost)

}
